package com.zzyl.nursing.controller;

import java.util.List;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 *
 * @author lao Chen
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备管理列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success("同步成功");
    }

    /**
     * 获取所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.findAllProduct());
    }

    /**
     * 新增IOT设备
     *
     * @param deviceDto }
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("register")
    @ApiOperation(value = "新增IOT设备")
    public AjaxResult add(@ApiParam(value = "设备管理实体", required = true) @RequestBody DeviceDto deviceDto) {
        deviceService.insert(deviceDto);
        return success("新增成功");
    }
    /**
     * 查看设备详情
     * @param iotId
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "查看设备详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataType = "String")})
    public AjaxResult detail(@PathVariable String iotId) {
        return success(deviceService.queryDeviceDetail(iotId));
    }
    /**
     * 查看设备上报数据
     */
    @GetMapping("/queryServiceProperties/{ioId}")
    @ApiOperation(value = "查看设备上报数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataType = "String")})
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable ("ioId") String iotId){
        List<DeviceShadowDataVo> voList = deviceService.queryServiceProperties(iotId);
        return R.ok(voList);
    }
    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{iotId}")
    @ApiOperation(value = "删除设备管理")
    public AjaxResult remove(@ApiParam(value = "物联网设备id", required = true) @PathVariable String iotId) {
        deviceService.deleteByioID(iotId);
        return success("删除成功");
    }
    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改设备管理")
    public AjaxResult edit(@ApiParam(value = "设备管理实体", required = true) @RequestBody DeviceDto deviceDto) {
        deviceService.updateDeviceInfo(deviceDto);
        return success("修改成功");
    }
}

